package com.unohacha.qqj.fzdr.util;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.unohacha.qqj.fzdr.R;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;


public class MyUtils {

    public static Dialog callUpdialDialog(Context context, String phoneNumber, View.OnClickListener clickListener){
        final Dialog dialog = new Dialog(context, R.style.MyDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.call_up_dialog);
        TextView telephone_number =  dialog.findViewById(R.id.telephone_number);
        telephone_number.setText(phoneNumber);
        dialog.findViewById(R.id.call_up).setOnClickListener(clickListener);
        dialog.findViewById(R.id.call_off).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
       
        return  dialog;
    }


    //判断是否为手机号 正则表达式校验
    public static boolean isPhone(String phone)throws PatternSyntaxException {
        String regExp = "^1[3|4|5|7|8|6|9][0-9]{9}$";
        Pattern pattern = Pattern.compile(regExp);
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }

    /**
     * 提供精确的小数位四舍五入处理.
     *
     * @param v
     * 需要四舍五入的数字
     * @param scale
     * 小数点后保留几位
     * @return 四舍五入后的结果
     */
    public static double round(Double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b = null == v ? new BigDecimal("0.0") : new BigDecimal(Double.toString(v));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}
